﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace AdoFramework
{
    public class BingFa
    {
        DBConnString db = new DBConnString();
        ManualResetEvent[] mre = new ManualResetEvent[5];
        public  BingFa() 
        {
            db.con1.Open();
            db.con2.Open();
            for (int i = 0; i < 5; i++)
            {
                mre[i] = new ManualResetEvent(false);
                //mre[i].Reset();
            }

        }
        public void Open()
        {
            for (int i = 0; i < 5; i++)
            {
                mre[i].Set();
            }
            
        }
        public void Start(int x)
        {
            Thread[] thArr = new Thread[x];
            for (int i = 0; i < x; i++)
            {
                if (i%2==0)
                {
                    thArr[i] = new Thread(new ParameterizedThreadStart( UpdataBingFa));
                }
                else
                {
                    thArr[i] = new Thread(UpdataBingFa2);
                }
                
                thArr[i].IsBackground = true;
            }

            for (int i = 0; i < x; i++)
            {
                thArr[i].Start(i);
            }
            //Thread t1 = new Thread(UpdataBingFa);
            //t1.IsBackground = true;

            //Thread t2 = new Thread(UpdataBingFa2);
            //t2.IsBackground = true;

            //t1.Start();
            //t2.Start();
        }

        private void UpdataBingFa(object threadNum)
        {

            string subsql = @"declare @result int
                            declare @num int
                            declare @tmp int
 
                            select @result = count(id) from tbluser  where id = 1 and num>0
                            
                            select @result,@num
                            if @result >0 
                            begin
                                select @num =num from tbluser where id = 1
                                set @tmp = @num -1
	                            update tbluser set num =@tmp  where id = 1

                                insert into tbluser(id,name,num)values(998,'success'," + threadNum.ToString() + @")
                            end
                            else
                            begin
                                insert into tbluser(id,name,num)values(998,'fail'," + threadNum.ToString() + @")
                            end
                            ";
            string sql = 
            @"SET TRANSACTION ISOLATION LEVEL read committed 
                                 begin tran " +
             subsql +
           " Commit tran";
            while (true)
            {
                mre[(int)threadNum].WaitOne();

                db.ProcessSql(sql, db.con1);
                mre[(int)threadNum].Reset();
            }
          
        }

        private void UpdataBingFa2(object threadNum)
        {
            string subsql = @"declare @result int
                            declare @num int
                            declare @tmp int
 
                            select @result = count(id) from tbluser    where id = 1 and num>0
                            

                            select @result,@num
                            if @result >0 
                            begin
                                select @num =num from tbluser where id = 1
                                set @tmp = @num -1
	                            update tbluser set num =@tmp  where id = 1

                                insert into tbluser(id,name,num)values(999,'success'," + threadNum.ToString() + @")
                            end
                            else
                            begin
                                insert into tbluser(id,name,num)values(998,'fail'," + threadNum.ToString() + @")
                            end
                            ";
            string sql =
                            @"SET TRANSACTION ISOLATION LEVEL read committed 
                             begin tran " +
                            subsql +
                          " Commit tran";
            while (true)
            {
                mre[(int)threadNum].WaitOne();

                db.ProcessSql(sql, db.con2);
                mre[(int)threadNum].Reset();
            }
          
        }
    }
}
